package com.sunlands.zlcx.usercenter.service.task;

import com.sunlands.zlcx.usercenter.common.annotation.AutoLog;
import com.sunlands.zlcx.usercenter.service.ApiRequestStatisticsServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class TaskApiRequestStatisticsServiceImpl {

    /**
     * 将接口请求次数和耗时持久化到mysql数据库
     */
    @AutoLog
    @Scheduled(cron = "0 0/1 * * * ?")
    public void refreshData() {
        log.info("将接口请求次数和耗时持久化到mysql数据库");
        commonTaskExecutor.execute(() ->
                ApiRequestStatisticsServiceImpl.requestRecordStore
                        .forEach(requestRecord ->
                                apiRequestStatisticsService.refresh(requestRecord)));
    }

    @Autowired
    private ApiRequestStatisticsServiceImpl apiRequestStatisticsService;

    @Resource(name = "commonTaskExecutor")
    private AsyncTaskExecutor commonTaskExecutor;
}